package com.lagou.configBean;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;

/**
 * @author ascetic
 * @version 1.0
 * @ClassName zkClient
 * @Description TODO
 * @date 2020-07-03 18:29
 */

public class ZkClient {


    private volatile static CuratorFramework  client;

    private ZkClient(CuratorFramework client) {
        this.client = client;
    }

    public static CuratorFramework getZKClient() {

        if(client==null){
            synchronized(ZkClient.class){
                RetryPolicy exponentialBackoffRetry = new ExponentialBackoffRetry(1000, 3);

                client = CuratorFrameworkFactory.builder()
                        .connectString("127.0.0.1:2181")
                        .sessionTimeoutMs(50000)
                        .connectionTimeoutMs(30000)
                        .retryPolicy(exponentialBackoffRetry)
                        .namespace("base")  // 独立的命名空间 /base
                        .build();

                client.start();
            }
        }

        return  client;
    }
}